package com.lwj.liwanjia.view.fragment.personal;


import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.base.BaseFragment;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.bean.CenterBean01;
import com.lwj.liwanjia.config.URLConstant;
import com.lwj.liwanjia.view.activity.address.AddressActivity;
import com.lwj.liwanjia.view.activity.login.LoginActivity;
import com.lwj.liwanjia.view.activity.myindent.MyIndentActivity;
import com.lwj.liwanjia.view.activity.myindent.ReturnActivity;
import com.lwj.liwanjia.view.activity.personal.FanKuiActivity;
import com.lwj.liwanjia.view.activity.personal.PersonalActivity;
import com.lwj.liwanjia.view.activity.personal.PinJiaActivity;
import com.lwj.liwanjia.view.activity.personal.qian_bao.QianBaoActivity;
import com.lwj.liwanjia.view.activity.personal.SheZhiActivity;
import com.lwj.liwanjia.view.activity.personal.ShouCangActivity;
import com.lwj.liwanjia.view.activity.personal.WoDeFaBuActivity;
import com.lwj.liwanjia.view.activity.personal.YaoQingActivity;
import com.lwj.liwanjia.view.activity.personal.ZuJiActivity;

import java.io.IOException;

import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 个人中心
 */
public class PersonalFragment extends BaseFragment implements View.OnClickListener {

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    startActivity(new Intent(getActivity(), LoginActivity.class).putExtra("type", "个人中心"));
                    getActivity().finish();
                    break;
                case 1:
                    String bangDingYuEr = bangDingYuErDouble + "";
                    if (!TextUtils.isEmpty(photoUrl)
                            && !TextUtils.isEmpty(name)
                            && !TextUtils.isEmpty(yuErDouble)
                            && !TextUtils.isEmpty(bangDingYuEr)
                            && !TextUtils.isEmpty(woDeShouCang)
                            && !TextUtils.isEmpty(woDeFaBu)) {
                        Glide.with(getActivity()).load(photoUrl).error(R.mipmap.headportrait).into(circle_photo);
                        tv_name.setText(name);
                        tv_yuEr.setText(yuErDouble);
                        tv_bangDingYuEr.setText(bangDingYuEr);
                        tv_shouCang.setText(woDeShouCang);
                        tv_faBu.setText(woDeFaBu);
                        tv_zuJi.setText(woDeZuJi);
                        if (isCheck01) {
                            ivDaifukuan.setImageResource(R.mipmap.mine_daifukuang);
                        } else {
                            ivDaifukuan.setImageResource(R.mipmap.daifukuang_1);
                        }
                        if (isCheck02) {
                            ivDaishouhuo.setImageResource(R.mipmap.mine_daishouhuo);
                        } else {
                            ivDaishouhuo.setImageResource(R.mipmap.daishouhuo);
                        }
                        if (isCheck03) {
                            ivYiwancheng.setImageResource(R.mipmap.mine_yiwancheng);
                        } else {
                            ivYiwancheng.setImageResource(R.mipmap.wancheng);
                        }
                        if (isCheck04) {
                            ivTuikuan.setImageResource(R.mipmap.mine_tuikuang);
                        } else {
                            ivTuikuan.setImageResource(R.mipmap.tuihuo);
                        }

                        //保存头像和名字
                        //SpUtils.putString(getActivity(),"centerUrl",photoUrl);
                        //SpUtils.putString(getActivity(),"centerName",name);
                    }

                    break;
            }
        }
    };

    private RelativeLayout rl_keFu;//客服容器
    private TextView tv_number;//客服Dialog里的电话
    private Dialog keFuDialog;//客服Dialog
    private RelativeLayout rl_fanKui;//反馈容器
    private LinearLayout ll_faBu;//发布的容器
    private LinearLayout ll_zuJi;//足迹
    private LinearLayout ll_shouCang;//收藏
    private ImageView iv_sheZhi;//设置
    private RelativeLayout rl_duiHuanJuan;//我的兑换券
    private RelativeLayout rl_pinJia;//我的评价
    private RelativeLayout rl_yaoQing;//我的邀请容器
    private RelativeLayout rl_jiFen;//我的积分
    private RelativeLayout rl_qianBao;//我的钱包
    private RelativeLayout rl_address;//地址管理
    private ImageView photo;//头像
    private CircleImageView circle_photo;//头像
    private TextView tv_yuEr;//余额
    private TextView tv_bangDingYuEr;//绑定余额
    private TextView tv_name;//名字
    private String photoUrl;
    private String name;
    private String yuErDouble;
    private double bangDingYuErDouble;
    private TextView tv_shouCang;//我的收藏
    private TextView tv_faBu;//我的发布
    private TextView tv_zuJi;//我的足迹
    private String woDeShouCang;
    private String woDeFaBu;
    private String woDeZuJi;
    private ImageView ivDaifukuan;
    private ImageView ivDaishouhuo;
    private ImageView ivYiwancheng;
    private ImageView ivTuikuan;
    private boolean isCheck01;
    private boolean isCheck02;
    private boolean isCheck03;
    private boolean isCheck04;
    public boolean isGuestLogin = false;

    @Override
    protected void initView(View view) {
        //寻找控件
        findView(view);
        //客服Dialog
        keFuDialog();
    }

    @Override
    protected void initData() {
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_personal;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //客服
            case R.id.rl_keFu:
                keFuDialog.show();
                break;
            //反馈
            case R.id.rl_fanKui:
                startActivity(new Intent(getActivity(), FanKuiActivity.class));
                break;
            //发布
            case R.id.ll_faBu:
                startActivity(new Intent(getActivity(), WoDeFaBuActivity.class));
                break;
            //足迹
            case R.id.ll_zuJi:
                startActivity(new Intent(getActivity(), ZuJiActivity.class));
                break;
            //收藏
            case R.id.ll_shouCang:
                startActivity(new Intent(getActivity(), ShouCangActivity.class));
                break;
            //设置
            case R.id.iv_sheZhi:
                startActivity(new Intent(getActivity(), SheZhiActivity.class));
                break;
            //兑换券
            case R.id.rl_duiHuanJuan:
//                startActivity(new Intent(getActivity(), DuiHuanJuanActivity.class));
                Toast.makeText(getActivity(), "暂未开通", Toast.LENGTH_SHORT).show();
                break;
            //评价
            case R.id.rl_pinJia:
                startActivity(new Intent(getActivity(), PinJiaActivity.class));
                break;
            //邀请
            case R.id.rl_yaoQing:
                startActivity(new Intent(getActivity(), YaoQingActivity.class));
                break;
            //积分
            case R.id.rl_jiFen:
//                startActivity(new Intent(getActivity(), JiFenActivity.class));
                Toast.makeText(getActivity(), "暂未开通", Toast.LENGTH_SHORT).show();
                break;
            //钱包
            case R.id.rl_qianBao:
                startActivity(new Intent(getActivity(), QianBaoActivity.class));
                break;
            //地址管理
            case R.id.rl_address:
                startActivity(new Intent(getActivity(), AddressActivity.class));
                break;
            //点击头像
            case R.id.circle_photo:
                Intent intent = new Intent(getActivity(), PersonalActivity.class);
                intent.putExtra("photo", photoUrl);
                intent.putExtra("nicheng", name);
                startActivity(intent);
                break;
            //客服电话
            case R.id.tv_number:
                Intent telephoneIntent = new Intent();
                telephoneIntent.setAction(Intent.ACTION_DIAL);
                telephoneIntent.setData(Uri.parse("tel:" + "010-88909123"));
                startActivity(telephoneIntent);
                keFuDialog.dismiss();
                break;
        }
    }

    //寻找控件
    private void findView(View view) {
        rl_keFu = (RelativeLayout) view.findViewById(R.id.rl_keFu);
        rl_fanKui = (RelativeLayout) view.findViewById(R.id.rl_fanKui);
        ll_faBu = (LinearLayout) view.findViewById(R.id.ll_faBu);
        ll_zuJi = (LinearLayout) view.findViewById(R.id.ll_zuJi);
        ll_shouCang = (LinearLayout) view.findViewById(R.id.ll_shouCang);
        iv_sheZhi = (ImageView) view.findViewById(R.id.iv_sheZhi);
        rl_duiHuanJuan = (RelativeLayout) view.findViewById(R.id.rl_duiHuanJuan);
        rl_pinJia = (RelativeLayout) view.findViewById(R.id.rl_pinJia);
        rl_yaoQing = (RelativeLayout) view.findViewById(R.id.rl_yaoQing);
        rl_jiFen = (RelativeLayout) view.findViewById(R.id.rl_jiFen);
        rl_qianBao = (RelativeLayout) view.findViewById(R.id.rl_qianBao);
        rl_address = (RelativeLayout) view.findViewById(R.id.rl_address);
        circle_photo = (CircleImageView) view.findViewById(R.id.circle_photo);
        tv_yuEr = (TextView) view.findViewById(R.id.tv_yuEr);
        tv_bangDingYuEr = (TextView) view.findViewById(R.id.tv_bangDingYuEr);
        tv_name = (TextView) view.findViewById(R.id.tv_name);
        tv_shouCang = (TextView) view.findViewById(R.id.tv_woDeShouCang1);
        tv_faBu = (TextView) view.findViewById(R.id.tv_woDeFaBu1);
        tv_zuJi = (TextView) view.findViewById(R.id.tv_woDeZuJi1);

        ivDaifukuan = (ImageView) view.findViewById(R.id.iv_daifukuan);
        ivDaishouhuo = (ImageView) view.findViewById(R.id.iv_daishouhuo);
        ivYiwancheng = (ImageView) view.findViewById(R.id.iv_yiwancheng);
        ivTuikuan = (ImageView) view.findViewById(R.id.iv_tuikuang);


        rl_keFu.setOnClickListener(this);
        rl_fanKui.setOnClickListener(this);
        ll_faBu.setOnClickListener(this);
        ll_zuJi.setOnClickListener(this);
        ll_shouCang.setOnClickListener(this);
        iv_sheZhi.setOnClickListener(this);
        rl_duiHuanJuan.setOnClickListener(this);
        rl_pinJia.setOnClickListener(this);
        rl_yaoQing.setOnClickListener(this);
        rl_jiFen.setOnClickListener(this);
        rl_qianBao.setOnClickListener(this);
        rl_address.setOnClickListener(this);
        circle_photo.setOnClickListener(this);

    }

    //客服Dialog
    private void keFuDialog() {

        keFuDialog = new Dialog(getContext(), R.style.DialogTheme);
        View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_ke_fu, null);
        keFuDialog.setContentView(dialogView);

        Window dialogWindow = keFuDialog.getWindow();
        DisplayMetrics dm = getActivity().getResources().getDisplayMetrics();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        dialogWindow.setGravity(Gravity.CENTER);

        tv_number = (TextView) dialogView.findViewById(R.id.tv_number);

        tv_number.setOnClickListener(this);
    }

    @OnClick({R.id.my_indent, R.id.daifukuan, R.id.daishouhuo, R.id.yiwancheng, R.id.tuihuo})
    public void onViewClicked(View view) {
        Intent intent = new Intent(getActivity(), MyIndentActivity.class);
        switch (view.getId()) {
            case R.id.my_indent://我的订单
                intent.putExtra("type", "0");
                startActivity(intent);
                break;
            case R.id.daifukuan://待付款
                intent.putExtra("type", "1");
                startActivity(intent);
                break;
            case R.id.daishouhuo://待收货
                intent.putExtra("type", "4");
                startActivity(intent);
                break;
            case R.id.yiwancheng://已完成
                intent.putExtra("type", "5");
                startActivity(intent);
                break;
            case R.id.tuihuo://退货
                Intent intent1 = new Intent(getActivity(), ReturnActivity.class);
                startActivity(intent1);
                break;
        }
    }

    //连网获取个人中心数据
    private void initCenter() {
        String url = URLConstant.BASE_URL + "oneCard/show";
        String token = SpfUtils.getSpfUtils(getActivity()).getToken();
        OkHttpClient mOkHttpClient = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("token", token)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call mCall = mOkHttpClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //Log.d("Tag", "个人中心数据获取失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //Log.d("Tag", "个人中心数据获取成功");

                String result = null;

                try {
                    result = response.body().string();
                    //Log.d("Tag", result);
                    parseCenter(result);
                } catch (Exception e) {

                }

            }
        });

    }

    //解析 个人中心 数据
    private void parseCenter(String result) {

        Gson gson = new Gson();
        CenterBean01 bean = gson.fromJson(result, CenterBean01.class);
        if (bean != null) {
            switch (bean.getState()) {

                case 0:
                    mHandler.sendEmptyMessage(0);
                    break;
                case 1:
                    photoUrl = bean.getData().getOneCardUser().getUserHeadImage();
                    name = bean.getData().getOneCardUser().getNick();
                    yuErDouble = bean.getData().getOneCardMoney();
                    bangDingYuErDouble = bean.getData().getOneCardBoundMoney();
                    woDeShouCang = bean.getData().getCollectSize() + "";
                    woDeFaBu = bean.getData().getPublishSize() + "";
                    woDeZuJi = bean.getData().getVisitSize() + "";
                    //woDeZuJi =
                    isCheck01 = bean.getData().isNoPay();
                    isCheck02 = bean.getData().isSend();
                    isCheck03 = bean.getData().isEnd();
                    isCheck04 = bean.getData().isBreturn();

                    mHandler.sendEmptyMessage(1);

                    break;
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        initCenter();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            initCenter();
        }
    }

}
